package com.bytech.common.biz.enums;

import com.bytech.common.biz.enums.comment.CommentTypeEnum;
import com.bytech.common.utils.StringUtils;
import com.google.common.collect.ImmutableList;
import lombok.Getter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author Zhao
 * @Create 2021/8/5 0:27
 */
@Getter
public enum ServiceTypeEnum {

    UNKNOWN_SERVICE("unknown-service", "未知微服务"),
    ARTICLE_SERVICE("service-blog-article", "文章"),
    USER_SERVICE("service-blog-user", "用户"),
    ADMIN_SERVICE("service-blog-admin", "统计"),
    COMMENT_SERVICE("service-blog-comment", "评论"),
    GATEWAY_SERVICE("service-blog-gateway", "网关"),
    ;

    private String serviceId;
    private String desc;

    ServiceTypeEnum(String serviceId, String desc) {
        this.serviceId = serviceId;
        this.desc = desc;
    }

    private static Map<String, ServiceTypeEnum> map = new HashMap<>(CommentTypeEnum.values().length);

    static {
        for (ServiceTypeEnum e : ServiceTypeEnum.values()) {
            map.put(e.serviceId, e);
        }
    }

    public static ServiceTypeEnum of(String type) {
        if (StringUtils.isBlank(type)) {
            return UNKNOWN_SERVICE;
        }
        return Optional.ofNullable(map.get(type)).orElse(UNKNOWN_SERVICE);
    }

    public static List<ServiceTypeEnum> validValues() {
        return ImmutableList.of(ARTICLE_SERVICE, USER_SERVICE, ADMIN_SERVICE, COMMENT_SERVICE, GATEWAY_SERVICE);
    }
}
